package com.yuqing.sentimentapp.channel.helper;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;

/**
 * Created by wangqingbang on 2017/3/15.
 *
 */
public class ItemDragHelperCallback extends ItemTouchHelper.Callback{
    /**
     * 设置滑动类型标记
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager || manager instanceof StaggeredGridLayoutManager){
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT ;
        }else {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }

        // 如果想支持滑动(删除)操作, swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END
        int swipeFlags = 0 ;

        //返回一个整数类型的标识，用于判断Item那种移动行为是允许的
        return makeMovementFlags(dragFlags,swipeFlags);
    }

    /**
     * 拖拽切换Item的回调
     * @return 如果Item切换了位置，返回true；反之，返回false
     * onMove()是在拖动到新位置时候的回调方法，我们在这里做数组集合的交换操作，在这里我们把它暴露出去，交给Adapter自己处理；
     */
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (viewHolder.getItemViewType() != target.getItemViewType()){
            return false;
        }
        //instanceof 这个对象是否是这个特定类或者是它的子类的一个实例。
        if (recyclerView.getAdapter() instanceof OnItemMoveListener){
            OnItemMoveListener listener = (OnItemMoveListener) recyclerView.getAdapter();
            listener.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
        }

        return true;
    }
    /**
     * 滑动删除Item的操作 这里我们暂时永不着
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }

    /**
     * item被选中时改变item的背景
     */
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        //不在闲置的状态
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE){
            if (viewHolder instanceof OnDragVHListener){
                OnDragVHListener itemViewHolder = (OnDragVHListener) viewHolder;
                itemViewHolder.onItemSelected();
            }
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    /**
     * 用户操作完毕或者动画完毕后调用，恢复item的背景和透明度
     */
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (viewHolder instanceof  OnDragVHListener){
            OnDragVHListener itemViewHolder = (OnDragVHListener) viewHolder;
            itemViewHolder.onItemFinished();
        }
        super.clearView(recyclerView, viewHolder);
    }

    /**
     * isLongPressDragEnabled()如果返回true，则支持长按拖拽，
     * 这里“其他频道”等不需要拖拽，所以返回false，手动调用ItemTouchHelper的startDrag方法启动拖拽。
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }

    /**
     *  不支持滑动
     * @return false
     */
    @Override
    public boolean isItemViewSwipeEnabled() {
        return false;
    }
}
